#include "nvgpumonitor.h"
#include <QProcess>

NVGpuMonitor::NVGpuMonitor()
{

}

NVGpuMonitor::~NVGpuMonitor()
{

}

double NVGpuMonitor::getGpuCoreUsage()
{
    QProcess process;
    process.start("nvidia-smi", {"--query-gpu=utilization.gpu",
                  "--format=csv,noheader,nounits"});
    process.waitForFinished();

    QString usage_str = QString(process.readAllStandardOutput());
    return usage_str.toDouble();
}

double NVGpuMonitor::getGpuMemUsage()
{
    QProcess process;
    process.start("nvidia-smi", {"--query-gpu=memory.used,memory.total",
                  "--format=csv,noheader,nounits"});
    process.waitForFinished();

    QStringList parts = QString(process.readAllStandardOutput()).trimmed().split(',');
    if (parts.size() >= 2) {
        double used_mem = parts[0].trimmed().toDouble();
        double total_mem = parts[1].trimmed().toDouble();

        double usage = (used_mem / total_mem) * 100;
        return usage;
    }
    return 0;
}
